// 使用express构建服务器api
const express = require("express");
const path = require("path");

const uploadController = require("./business/upload/upload.controller.js");

const app = express();

const PORT = process.env.PORT || 8080;

// 处理所有响应，设置跨域
app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With ");
  res.header("X-Powered-By", " 3.2.1");
  // res.header("Content-Type", "application/json;charset=utf-8");

  console.log("请求路径：" + req.url);

  next();
});

/** 静态文件 */
app.use("/static", express.static("./static"));
app.use("/upload", express.static("./upload"));

app.use("/api/upload", uploadController);

/** 处理404 */
app.use((req, res) => {
  res.status(404).send({
    code: 404,
    msg: "请求的资源不存在",
    path: req.url,
  });
});

// 开启本地端口侦听
app.listen(PORT, () => {
  console.log(`[服务器启动成功] ${PORT}`);
  console.log(`[静态页地址]: http://localhost:${PORT}/static`);
});
